public class Date {
    private int month;
    private int date;
    private int year;

    private Date next;

    public Date() {}

    public Date(int month, int date, int year) {
        this.month = month;
        this.date = date;
        this.year = year;
    }

    @Override
    public String toString() {
        return String.format("%s-%s-%s", year, month, date);
    }

    private Boolean isInRange(int v, int min, int max) {
        if (v < min || v > max) {
            System.out.println("Value of " + v + " not in the range " + min + "-" + max);
            return false;
        }
        return true;
    }

    private void update(int maxDate) {
        if (date == maxDate) {
            next.date = 1;
            if (month == 12) {
                next.month = 1;
                next.year++;
            } else {
                next.month++;
            }
        } else {
            next.date++;
        }
    }

    public String next() {
        if (!isInRange(month, 1, 12))
            return "Invalid Input Date";

        next = new Date(month, date, year);

        switch (month) {
            case 1, 3, 5, 7, 8, 10, 12 -> {
                if (isInRange(date, 1, 31)) {
                    update(31);
                } else {
                    return "Invalid Input Date";
                }
            }
            case 4, 6, 9, 11 -> {
                if (isInRange(date, 1, 30))
                    update(30);
                else
                    return "Invalid Input Date";
            }
            case 2 -> {
                if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
                    if (isInRange(date, 1, 29))
                        update(29);
                    else
                        return "Invalid Input Date";
                } else if (isInRange(date, 1, 28))
                    update(28);
                else
                    return "Invalid Input Date";
            }
        }

        System.out.println(next);
        return next.toString();
    }
}
